package org.mogu.service;


import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultHttpClient;
import org.mogu.db.ActiveUserHelper;
import org.mogu.db.DBhelper;

import org.mogu.R;
import org.mogu.TMain;
import org.mogu.Welcome;

import android.app.Service;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.IBinder;
import android.util.Log;
import android.app.Notification;

import android.app.NotificationManager;

import android.app.PendingIntent;



import android.os.Binder;

import android.os.Handler;



public class ContentService extends Service implements IInfoService{
	InfoSync infoSync=new InfoSync();
	DefaultHttpClient client;
	HttpResponse httpResponse;
	ActiveUserHelper auh;
	SQLiteDatabase readdb;
	SQLiteDatabase writedb;

	String updateTime;
	
	Context context;
	
	ContentValues value;
	Intent i;
	/**

	　　* 创建Handler对象，作为进程传递postDelayed之用

	　　*/



	private static final String TAG = "TService";

	private NotificationManager notificationManager;

	

	public void onCreate() {

	Log.d(TAG, "============> TService.onCreate");
	context=this;
	auh = DBhelper.getDBHelper(this);
	readdb=auh.getReadableDatabase();
	Cursor activelist=readdb.rawQuery("select * from activeuser ", null);
	int count=activelist.getCount();
	activelist.close();
	//readdb.close();
	//auh.close();
	if(count==0){
		i=new Intent(ContentService.this,TMain.class);
	}else{
		i=new Intent(ContentService.this,Welcome.class);
	}
	
	i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

	showNotification();

	super.onCreate();
	
	}
	private Handler handler = new Handler();  
//    private Runnable runnable;

	public void onStart(Intent intent, int startId) {

	Log.i(TAG, "============> TService.onStart");

	
	super.onStart(intent, startId);
//	if (auh==null){
//		auh=DBhelper.getDBHelper(this);
//	}
	new Thread(){
		public void run(){
			auh=DBhelper.getDBHelper(context);
			sync(auh);//要定时执行的任务  
			//auh.close();
            handler.postDelayed(this, 1000*30*3);  
		}
	}.start();
//	runnable = new Runnable() {  
//        public void run() {  
//
//        sync(auh);//要定时执行的任务  
//
//              handler.postDelayed(this, 1000*60*3);  
//
//          }  
//
//   }; 
//   runnable.run();
	
	}
	




	public IBinder onBind(Intent intent) {

	Log.i(TAG, "============> TService.onBind");

	return serviceBinder;
	}

	public class LocalBinder extends Binder {

	public ContentService getService() {

	return ContentService.this;
	}

	}

	public boolean onUnbind(Intent intent) {

	Log.i(TAG, "============> TService.onUnbind");

	return false;
	}

	public void onRebind(Intent intent) {

	Log.i(TAG, "============> TService.onRebind");
	}

	public void onDestroy() {

	Log.i(TAG, "============> TService.onDestroy");

	notificationManager.cancel(R.string.service_start);

	

	super.onDestroy();
	}

	private void showNotification() {

		NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);              
		Notification n = new Notification(R.drawable.moguico, "蘑菇咨询已经启动", System.currentTimeMillis());            
		n.flags = Notification.FLAG_AUTO_CANCEL;               
		//Intent i = new Intent(arg0.getContext(), NotificationShow.class);
		//i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);          
		//PendingIntent
		PendingIntent contentIntent = PendingIntent.getActivity(
		        this,
		        R.string.app_name,
		        i,
		        PendingIntent.FLAG_UPDATE_CURRENT);
			                 
		n.setLatestEventInfo(
		        this,
		        "蘑菇咨询",
		        "蘑菇咨询已经启动。",
		        contentIntent);
		nm.notify(R.string.app_name, n);
	}

	
	
	public void onDestory(){
		
	}
	
	public void sync(ActiveUserHelper auh) {
		

//			ActiveUserHelper auh = DBhelper.getDBHelper(this);

			int newcount=0;//=new InfoSync();//.sync(this,auh);
//			int newcount=infoSync.sync( readdb, wdb);
			if(newcount==0){
				return;
			}
				NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);              
				Notification n = new Notification(R.drawable.moguico, "蘑菇咨询", System.currentTimeMillis());            
				n.flags = Notification.FLAG_AUTO_CANCEL;               
				//Intent i = new Intent(arg0.getContext(), NotificationShow.class);
				//i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);          
				//PendingIntent
				PendingIntent contentIntent = PendingIntent.getActivity(
				        this,
				        R.string.app_name,
				        i,
				        PendingIntent.FLAG_UPDATE_CURRENT);
					                 
				n.setLatestEventInfo(
				        this,
				        "新收到咨询！",
				        "新收到"+newcount+"条。",
				        contentIntent);
				nm.notify(R.string.app_name, n);
	}



	private int count=1;

	private ServiceBinder serviceBinder=new ServiceBinder();

	public class ServiceBinder extends Binder implements IInfoService{
	@Override
	public int getCount() {
		return count;

	}

	} 

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 0;
	}
	
}
